package com.tjbank.cssys.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.primitives.Ints;
import com.tjbank.cssys.domain.CkCreditOutCompanyData;
import com.tjbank.cssys.domain.CkCreditOutCompanyDataRelation;
import com.tjbank.cssys.repository.CkCreditOutCompanyDataLogMapper;
import com.tjbank.cssys.repository.CkCreditOutCompanyDataMapper;
import com.tjbank.cssys.repository.CkCreditOutCompanyDataRelationMapper;
import com.tjbank.cssys.repository.CkOutsourcingCompanyMapper;
import com.tjbank.cssys.web.service.CkCreditOutCompanyDataService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CkCreditOutCompanyDataServiceImpl implements CkCreditOutCompanyDataService {
    protected static final Logger logger = LoggerFactory.getLogger(CkCreditOutCompanyDataServiceImpl.class);

    @Autowired
    private CkCreditOutCompanyDataMapper ckCreditOutCompanyDataMapper;
    @Autowired
    private CkOutsourcingCompanyMapper ckOutsourcingCompanyMapper;
    @Autowired
    private CkCreditOutCompanyDataLogMapper ckCreditOutCompanyDataLogMapper;
    @Autowired
    private CkCreditOutCompanyDataRelationMapper ckCreditOutCompanyDataRelationMapper;

    @Override
    public PageInfo getAllotCompanyData(Map<String, String> params) {
        logger.info("---查看委外公司案件数start---params" + JSON.toJSONString(params));
        int page = Integer.parseInt(params.getOrDefault("page", "1"));
        int pageSize = Integer.parseInt(params.getOrDefault("pageSize", "10"));
        PageHelper.startPage(page, pageSize);
        String batchNumber = DateUtils.getBatchNumber();
        params.put("batchNumber", batchNumber);
        List<Map<String, Object>> resultMap = ckCreditOutCompanyDataMapper.getAllotCompanyData(params);
        logger.info("---查看委外公司案件数end---result" + JSON.toJSONString(resultMap));
        PageInfo pageInfo = new PageInfo<>(resultMap);
        return pageInfo;
    }

    @Override
    public PageInfo getOutCompanyDataInfomation(Map<String, String> map) {
        logger.info("---查询委外公司详情start---" + JSON.toJSONString(map));
        int page = Integer.parseInt(map.getOrDefault("page", "1"));
        int pageSize = Integer.parseInt(map.getOrDefault("pageSize", "10"));
        PageHelper.startPage(page, pageSize);
        List<Map<String, Object>> resultMap = ckCreditOutCompanyDataMapper.getOurCompanyDataInformation(map);
        logger.info("---查询委外公司详情end---" + JSON.toJSONString(resultMap));
        PageInfo pageInfo = new PageInfo<>(resultMap);
        return pageInfo;
    }

    @Override
    public CommonResult updateComapnyNameNull(Map<String, Object> map) {
        logger.info("---修改已经分配好的委外案件start---params;" + JSON.toJSONString(map));
        if (null == map || map.size() == 0) {
            return CommonResult.success("参数是空");
        }
        String params = map.get("ids").toString();
        String[] strings = params.split(",");
        int[] arr = Arrays.asList(strings).stream().mapToInt(Integer::parseInt).toArray();
        List<Integer> list = Ints.asList(arr);
        map.put("list", list);
        ckCreditOutCompanyDataMapper.setCompanyName(map);
        logger.info("---修改已经分配好的委外案件end---success");
        return CommonResult.success("修改成功");
    }

    @Override
    public PageInfo getOutCompanyDataIsNull(Map<String, String> map) {
        logger.info("---查询公共池中的委外案件start---params：" + JSON.toJSONString(map));
        int page = Integer.parseInt(map.getOrDefault("page", "1"));
        int pageSize = Integer.parseInt(map.getOrDefault("pageSize", "10"));
        PageHelper.startPage(page, pageSize);
        String batchNumber = DateUtils.getBatchNumber();
        map.put("batchNumber", batchNumber);
        List<Map<String, Object>> resultMap = ckCreditOutCompanyDataMapper.getOutCompanyDataIsNull(map);
        logger.info("------查询公共池中的委外案件end---result:" + JSON.toJSONString(resultMap));
        PageInfo pageInfo = new PageInfo<>(resultMap);
        return pageInfo;
    }

    @Override
    public CommonResult getAllOutComapnyByCaseType(Map<String, String> map) {
        List<Map<String, String>> listMap = ckOutsourcingCompanyMapper.getAllOutComapnyBycaseType(map);
        return CommonResult.success(listMap);
    }

    @Override
    @Transactional
    public CommonResult exprictOutCompanyData(Map<String, String> map) {
        logger.info("---下发委外公司数据start--params：" + JSON.toJSONString(map));
        Map<String, Object> outDataRelation = ckCreditOutCompanyDataMapper.getOutDataRelation(map);
        CkCreditOutCompanyDataRelation ckCreditOutCompanyDataRelation = handlePo(outDataRelation, map);
        List<CkCreditOutCompanyData> list = ckCreditOutCompanyDataMapper.selectListByoutCompanyName(map);
        ckCreditOutCompanyDataRelationMapper.insert(ckCreditOutCompanyDataRelation);
        ckCreditOutCompanyDataMapper.deleteOutCompanyDataByCompanyName(map);
        ckCreditOutCompanyDataMapper.insertBatchOutCompanyDataLog(list);
        logger.info("---下发委外公司数据eng---result:" + JSON.toJSONString(list));
        list.clear();
        ckCreditOutCompanyDataRelation=null;
        return CommonResult.success("success");
    }

    @Override
    @Transactional
    public void receivedOutCompanyData(Map<String, String> map) {
        logger.info("---撤回数据--start--params:" + JSON.toJSONString(map));
        int count = ckCreditOutCompanyDataLogMapper.updateRecellStatue(map);
        int result = ckCreditOutCompanyDataRelationMapper.updateReceive(map);
        logger.info("---撤回数据--end--success " + JSON.toJSONString(map));
    }


    private CkCreditOutCompanyDataRelation handlePo(Map<String, Object> outDataRelation, Map<String, String> param) {
        CkCreditOutCompanyDataRelation ckCreditOutCompanyDataRelation = new CkCreditOutCompanyDataRelation();
        Object object= outDataRelation.get("allCount");
        long result= (long) object;
        ckCreditOutCompanyDataRelation.setAnjianCount((int) result);
        ckCreditOutCompanyDataRelation.setAllotDate(new Date(new Date().getTime()));
        ckCreditOutCompanyDataRelation.setBatchNumber(param.get("batchNumber"));
        ckCreditOutCompanyDataRelation.setOutsourcingCompany(String.valueOf(outDataRelation.get("out_company_name")));
        ckCreditOutCompanyDataRelation.setCheckModel(String.valueOf(outDataRelation.get("check_model")));
        ckCreditOutCompanyDataRelation.setFileName(param.get("batchNumber") +"-"+String.valueOf(outDataRelation.get("out_company_name"))+".xlsx");
        ckCreditOutCompanyDataRelation.setAnjianState("1");
        ckCreditOutCompanyDataRelation.setIsCalculated(2);
        return ckCreditOutCompanyDataRelation;
    }

}
